#http://ydyl.drcnet.com.cn
from django.shortcuts import render

# Create your views here.
from django.http import HttpResponse
import json
import requests
import time
from bs4 import BeautifulSoup
import time
import re
import json
import urllib
proxies = {
            "http": "http://qf:qf3.1415@202.112.118.7:10086/"
        }
#返回结果格式
def resultData():
    data = {
        "websiteType" : 0,
        "titleName":"",
        "titleHref": "",
        "source":"",
        "label":"",
        "volume":"",
        "summary":"",
        "detailId":'',
        "publishTime" :"",
        "category" : "",
        "quote" :"",
        "abstract": "",
        "doi": "",
        "downLoad": "",
        "keyword": [],
        "author": [],

    }
    return data

def formData(response):
    allData = []
    # 获取sid并且更新返回
    soup = BeautifulSoup(response.text, "html5lib")


    lis = soup.select(".one")


    for li in lis:
        tempData = resultData()

        try:
            tempData["titleName"] = li.select(".aa")[0].text
        except:
            pass
        try:
            tempData["titleHref"] = li.select(".aa")[0]["href"]
        except:
            pass
        # try:
        #     tempData["keyword"] = [subtitles[i].select("u")[0].text]
        # except:
        #     pass
        try:
            tempData["publishTime"] = li.select(".t1")[0].text
        except:
            pass
        try:
            tempData["abstract"] = li.select(".t2")[0].text
        except:
            pass

        allData.append(tempData)

    return allData

def getData(keyword,cookies):


    headers = {
        'Connection': 'keep-alive',
        'Upgrade-Insecure-Requests': '1',
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36',
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
        'Referer': 'http://tt.sciinfo.cn/ThinkTank/Result.aspx?q=TYPE:(M%20OR%20Z%20OR%20R%20OR%20B%20OR%20E%20OR%20C)%20AND%20(cell%20OR%20%E7%BB%86%E8%83%9E)',
        'Accept-Encoding': 'gzip, deflate',
        'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
    }

    params = (
        ('q', 'TYPE:(M OR Z OR R OR B OR E OR C) AND ({} OR {})'.format(keyword,keyword)),
    )

    response = requests.get('http://tt.sciinfo.cn/ThinkTank/Result.aspx', headers=headers, params=params,cookies = cookies,proxies = proxies)

    allData = formData(response)

    return allData

def getCookies():

    headers = {
        'Upgrade-Insecure-Requests': '1',
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36',
    }
    s = requests.session()

    response = s.get('http://tt.sciinfo.cn/ThinkTank/thinkindex.aspx', headers=headers,proxies = proxies)

    cookies = s.cookies.get_dict()

    return cookies

def getOneCookies():

    headers = {
        'Proxy-Connection': 'keep-alive',
        'Upgrade-Insecure-Requests': '1',
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36',
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
        'Accept-Encoding': 'gzip, deflate',
        'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
    }

    s = requests.session()

    s.get('http://cndata.datesdata.com.cn/', headers=headers,proxies = proxies)

    cookies = s.cookies.get_dict()

    return cookies


def index(request):

    data = json.loads(request.POST.get('data', '1'))

    cookies = getCookies()

    allData = getData(data["values"][0],cookies)

    return HttpResponse(json.dumps({
        "collection": allData,
        "allNum": 0
    }), content_type="application/json")